nwnfandomcom-20200213-history
Bitwise operator
A bitwise operator manipulates the digits in the binary representation of an integer. ("Bit" is short for "b''inary dig''it".) The bitwise operators in NWScript are &', '^, |', and '~ (not to be confused with the logical operators && and ||). These operators are typically only used when dealing with bit fields, as their operation can seem arbitrary or even unpredictable in other contexts. (That is assuming "normal" usage of NWScript; they are useful in other contexts when considering computer programming in general.) More precisely, bitwise operators are usually only appropriate when the primary concern is of the values of bits (as is the case with bit fields), not when the primary concern is the numeric value of the integer. The bitwise-and operator (&) compares the bits in two integers, and produces an integer in which each bit is 1 if both corresponding bits in the operands are 1, and is 0 otherwise. That means the evaluation of 3 & 5, which is written 0011 & 0101 in binary, is 0001 in binary (or 1 in decimal). The bitwise-exclusive-or operator (^) compares the bits in two integers, and produces an integer whose bits are 1 if exactly one of the corresponding bits in the operands is 1, and are 0 otherwise. That means the evaluation of 3 ^ 5, which is written 0011 ^ 0101 in binary, is 0110 in binary (or 6 in decimal). The bitwise-or operator (|) compares the bits in two integers, and produces an integer whose bits are 1 if either corresponding bit in the operands is 1, and are 0 otherwise. That means the evaluation of 3 | 5, which is written 0011 | 0101 in binary, is 0111 in binary (or 7 in decimal). The bitwise-negation operator (~) toggles the bits in a single integer, changing each 0 to 1 and each 1 to 0. That means the evaluation of ~5, which is written ~0101 in (4-bit) binary is 1010 in 4-bit binary (or −6 in decimal). The actual mechanics used in NWScript involve 32 bits instead of just 4, but the end value is the same. (Specifically, ~5 would be ~0000 0000 0000 0000 0000 0000 0000 0101 in 32-bit binary, which evaluates to 1111 1111 1111 1111 1111 1111 1111 1010, which is still −6 due to the scheme used to represent negative integers). Some scripts will function as expected if a bitwise-and or bitwise-or is used in place of the corresponding logical operator. This is because if dealing with just the values TRUE and FALSE, bitwise and logical operators produce the same result. However, if other values are introduced, then scripts that mistakenly use a bitwise operator are likely to fail. For example, while 6 && TRUE evaluates to TRUE, the bitwise version, 6 & TRUE, evaluates to FALSE (since 6 & TRUE is 0110 & 0001 in binary, which evaluates to 0000). When dealing with bit fields, bitwise-or is the operator used to set a flag, while bitwise-and is the operator used to retrieve a flag. Toggling a flag can be done with the exclusive-or. Clearing a flag is the more complicated operation, as it involves both bitwise-negation and bitwise-and. Specifically, field & ~flag will clear the flag in the field. category:operators